#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @Version : Python-
# @Author  : Shengji He
# @Email   : hsjbit@163.com
# @File    : BinaryTreePreorderTraversal.py
# @Time    : 2020/10/27 9:51
# @Description:
from typing import List


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        """
        Given the root of a binary tree, return the preorder traversal of its nodes' values.

        :param root:
        :return:
        """
        ans = []
        def helper(root):
            nonlocal ans
            if not root:
                return
            ans.append(root.val)
            helper(root.left)
            helper(root.right)
        helper(root)
        return ans


if __name__ == '__main__':
    root = TreeNode(1)
    root.right = TreeNode(2)
    root.right.left = TreeNode(3)
    S = Solution()
    print(S.preorderTraversal(root))
    print('done')
